package avicit.bdp.dds.apc.service;

import avicit.bdp.common.utils.SpringApplicationContext;
import avicit.bdp.dds.apc.dao.PythonResourceLogDao;
import avicit.bdp.dds.apc.dto.PythonResourceLogDTO;
import avicit.bdp.dds.remote.command.ShellExecuteRequestCommand;
import avicit.bdp.dds.remote.utils.Host;
import avicit.bdp.dds.server.master.dispatch.context.ExecutionContext;
import avicit.bdp.dds.server.master.dispatch.enums.ExecutorType;
import avicit.bdp.dds.server.master.dispatch.executor.NettyExecutorManager;
import avicit.bdp.dds.server.registry.ZookeeperNodeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @金航数码科技有限责任公司 @作者：liyb @邮箱：liyb@avic-digital.com @创建时间：2021-05-14 10:29 @类说明：python包管理 @修改记录：
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PythonResService {

  private static final Logger logger = LoggerFactory.getLogger(PythonResService.class);

  @Autowired private ZookeeperNodeManager zookeeperNodeManager;
  @Autowired private PythonResourceLogDao pythonResourceLogDao;

  /**
   * 在线安装
   *
   * @param pyScript
   * @param address
   * @return
   * @throws Exception
   */
  public Boolean onlineInstall(String pyScript, String address, String logId) throws Exception {
    ShellExecuteRequestCommand requestCommand = new ShellExecuteRequestCommand();
    requestCommand.setPyScript(pyScript);
    requestCommand.setOnline(1);
    requestCommand.setLogId(logId);

    ExecutionContext executionContext =
        new ExecutionContext(requestCommand.convert2Command(), ExecutorType.WORKER);

    String[] arr = address.split(":");
    Host host = new Host();
    host.setIp(arr[0]);
    host.setPort(Integer.parseInt(arr[1]));
    executionContext.setHost(host);

    NettyExecutorManager nettyExecutorManager =
        SpringApplicationContext.getBean(NettyExecutorManager.class);
    nettyExecutorManager.executeDirectly(executionContext);
    return true;
  }

  /**
   * 离线安装
   *
   * @param resourceId
   * @param address
   * @return
   * @throws Exception
   */
  public Boolean offlineInstall(String resourceId, String address, String logId) throws Exception {
    ShellExecuteRequestCommand requestCommand = new ShellExecuteRequestCommand();
    requestCommand.setResourceId(resourceId);
    requestCommand.setOnline(0);
    requestCommand.setLogId(logId);

    ExecutionContext executionContext =
        new ExecutionContext(requestCommand.convert2Command(), ExecutorType.WORKER);

    String[] arr = address.split(":");
    Host host = new Host();
    host.setIp(arr[0]);
    host.setPort(Integer.parseInt(arr[1]));
    executionContext.setHost(host);

    NettyExecutorManager nettyExecutorManager =
        SpringApplicationContext.getBean(NettyExecutorManager.class);
    nettyExecutorManager.executeDirectly(executionContext);
    return true;
  }

  public void inserResourceLog(String id, String context) {
    PythonResourceLogDTO pythonResourceLog = new PythonResourceLogDTO();
    pythonResourceLog.setId(id);
    pythonResourceLog.setContext(context);
    pythonResourceLogDao.insert(pythonResourceLog);
  }

  public String getResourceLog(String logId) {
    PythonResourceLogDTO pythonResourceLog = pythonResourceLogDao.selectByPrimaryKey(logId);
    if (pythonResourceLog != null) {
      return pythonResourceLog.getContext();
    }
    return null;
  }

  public PythonResourceLogDTO getResourceLogById(String logId) {
    return pythonResourceLogDao.selectByPrimaryKey(logId);
  }

  public void updateResourceLogById(PythonResourceLogDTO pythonResourceLog, String logs) {
    pythonResourceLog.setContext(pythonResourceLog.getContext() + "\n\t" + logs);
    pythonResourceLogDao.updateByPrimaryKey(pythonResourceLog);
  }
}
